package ca.mark.mycode.sqlite;

import java.io.File;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author tiantian
 * 
 */

public class SqliteOperation {

	private SQLiteDatabase mDatabase;
	private Context mContext;

	private final static String DB_NAME = "markDB";
	private final String TABLE_NAME = "users";
	private final String USERNAME = "userName";
	private final String USERADDRESS = "userAddress";

	public SqliteOperation(Context context) {
		mContext = context;
	}

	public void openDB() {
		mDatabase = mContext.openOrCreateDatabase(DB_NAME,
				mContext.MODE_PRIVATE, null);
		
		

		// if the Table doesn't exist, create it

		try {
			
				Log.v("Mark wang", "Create Table " + TABLE_NAME);

				/*
				 * mDatabase.execSQL("create TABLE" + TABLE_NAME +
				 * "(userId INTEGER PRIMARY KEY AUTO INCREASE," + USERNAME +
				 * "VARCHAR(50) NOT NULL," + USERADDRESS +
				 * "userAddress VARCHAR(50) NOT NULL");
				 */
				mDatabase.execSQL("create TABLE IF NOT EXISTS " + TABLE_NAME
						+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
						+ USERNAME + " VARCHAR(50) NOT NULL, " + USERADDRESS
						+ " VARCHAR(50) NOT NULL )");
			
		} catch (Exception e) {
			// TODO: handle exception
			Log.v("Mark wang", "Create Table " + TABLE_NAME+ "exit!");
		}
		

		// insert a record for test
		/*
		 * ContentValues values=new ContentValues(); values.put(USERNAME,
		 * "Mark"); values.put(USERADDRESS, "BeiJing");
		 * mDatabase.insert(TABLE_NAME, null, values);
		 */

		//mDatabase.close();
		// Release completely
		// mDatabase=null;

	}

	public long inserUsers(User user) {

		if (mDatabase != null && user != null) {
			if (!mDatabase.isOpen()) {
				
				File _path = mContext.getDatabasePath(DB_NAME);
				mDatabase.openDatabase(_path.getAbsolutePath(), null,
						SQLiteDatabase.OPEN_READWRITE);
				Log.v("Mark wang", "open database "+_path.getAbsolutePath());

			}
			
			Log.v("Mark wang", "Insert record ");
			ContentValues _tempValues = new ContentValues();
			_tempValues.put(USERNAME, user.getUserName());
			_tempValues.put(USERADDRESS, user.getUserAddress());

			long i = mDatabase.insert(TABLE_NAME, "", _tempValues);
			queryall();
			//mDatabase.close();
			return i;

		} else
			return -1;
	}

	public void queryall() {
		if (!mDatabase.isOpen()) {
			File _path = mContext.getDatabasePath(DB_NAME);
			mDatabase.openDatabase(_path.getAbsolutePath(), null,
					SQLiteDatabase.OPEN_READWRITE);
		}

		Cursor cursor = mDatabase.query(TABLE_NAME, null, null, null, null,
				null, USERNAME);
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			Log.v("Mark wang", "The " + cursor.getInt(0) + " record " + USERNAME
					+ " is " + cursor.getString(1) + " "+ USERADDRESS + " is "
					+ cursor.getString(2));

			
		}
		//mDatabase.close();

	}
	
	public void closeDB(){
		mDatabase.close();
		mDatabase=null;
	}

}